import scrapy
from myspider.items import MyspiderItem


class ItcastSpider(scrapy.Spider):
    name = "itcast"
    allowed_domains = ["itcast.cn"]
    start_urls = ["https://itcast.cn/channel/teacher.shtml#ajavaee/"]

    def parse(self, response):
        # 获取 所有教师节点
        nodelist = response.xpath('//div[@class="li_txt"]')
        # 遍历 教师节点
        for node in nodelist:
            # item = {}
            # 实例化
            item = MyspiderItem()

            item['name'] = node.xpath('./h3/text()').extract_first() #适合只含一个值的列表
            item['title'] = node.xpath('./h4/text()')[0].extract() #适合含有多个值的列表
            item['desc'] = node.xpath('./p/text()')[0].extract()

            yield item  #Scrapy能继续执行后续代码，实现"边爬取边处理"。
